package com.cqtczh.qhd.driver.acticity.carteam;

import httptools.HttpJsonTool;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tools.L;

import javabeen.CarTeamIdBeen;
import javabeen.UserInfoBeen;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.acticity.base.BaseActivity;
import com.cqtczh.qhd.driver.acticity.order.OrderDetails;
import com.cqtczh.qhd.driver.api.GagApi;
import com.cqtczh.qhd.driver.model.CarTeam;
public class JionCarTeamInfo extends BaseActivity {
	private CarTeamIdBeen Jionteam;
	private ListView listView;
	private TextView carteamname;
	private TextView leadername;
	private TextView leaderphone;
	private TextView carteamdetil;
	private AppContent app;
	private Button button;
	private String leaderid;

	private List<CarTeamIdBeen> car_lead;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_team_info);
		app = (AppContent) getApplication();
		
		Ini();
		GetJoinCarTeam();
	}

	@SuppressWarnings("unchecked")
	private void Ini() {
		
		Intent intent = getIntent();
		leaderid = intent.getStringExtra("leaderid");
		TextView textView=(TextView) findViewById(R.id.head_title);
		textView.setVisibility(View.VISIBLE);
		textView.setText("您加入的车队信息");
		ImageView imageView=(ImageView) findViewById(R.id.head_left);
		imageView.setVisibility(View.VISIBLE);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				JionCarTeamInfo.this.finish();
			}
		});
		Jionteam = (CarTeamIdBeen) intent
				.getSerializableExtra("jioncarteaminfo");
		if (Jionteam == null) {
			return;
		}

		carteamname = (TextView) findViewById(R.id.ctextVew1);
		carteamname.setText("车队名字："+Jionteam.getMotorcadename());
		

		
	
		carteamdetil = (TextView) findViewById(R.id.ctextVew2);
		if ((Jionteam.getMotorcadedescription()!=null)&&(!Jionteam.getMotorcadedescription().equals(""))) {
			carteamdetil.setText(Jionteam.getMotorcadedescription());
		}else {
			View view=findViewById(R.id.carteam_detail);
			view.setVisibility(View.GONE);
			carteamdetil.setVisibility(View.GONE);
		}
		
		
		button = (Button) findViewById(R.id.chek);
		button.setVisibility(View.VISIBLE);
		button.setText("退出车队");
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				exitCarTeamShowDialog("您是否需要退出车队？");
			}
		});
	}
	
	
		private void GetJoinCarTeam() {
			//app = (AppContent) getApplication();
			showLoadingDialog("加载中");
			UserInfoBeen userInfoBeen=app.userInfoBeen;
			Type type=new TypeToken<List<CarTeamIdBeen>>(){}.getType();
			Map<String, Object> dataMap=new HashMap<String, Object>();
			dataMap.put("leaderid", Jionteam.getLeaderid()+"");
			HttpJsonTool httpJsonTool=new HttpJsonTool(this);
			httpJsonTool.SetOnLister(new Handler(){
				@Override
				public void handleMessage(Message msg) {
					// TODO Auto-generated method stub
					super.handleMessage(msg);
					if (msg.what==1) {
					//	Toast.makeText(CarTeamConter.this, "获取到车辆信息", Toast.LENGTH_LONG).show();
						car_lead=(List<CarTeamIdBeen>) msg.obj;
						if (car_lead.size()>0) {
							iniLeaderInfo();
						}
//						Intent intent=new Intent(CarTeamConter.this,JionCarTeamInfo.class);
//						intent.putExtra("jioncarteaminfo", Jionteam.get(0));
//						intent.putExtra("leaderid", Jionteam.get(0).getLeaderid()+"");
//						startActivity(intent);
					}else {
						dismissLoadingDialog();
//						Toast.makeText(JionCarTeamInfo.this, (String)msg.obj, Toast.LENGTH_LONG).show();
					}
				
				}
			});
			httpJsonTool.StartWork(dataMap, GagApi.GET_JOIN_CAR_TEAM, type, null);
		}
	
	
	private void iniLeaderInfo(){
		dismissLoadingDialog();
		leaderphone = (TextView) findViewById(R.id.ctextVew4);
		leaderphone.setText("车队长电话："+car_lead.get(0).getPhone());
		leadername = (TextView) findViewById(R.id.ctextVew3);
		if ((Jionteam.getCarownername()!=null)&&(!Jionteam.getCarownername().equals(""))) {
			leadername.setText("车队队长名字："+car_lead.get(0).getCarownername());
		}else {
			leadername.setText("车队队长名字：队长未输入名字");
		}
	}
		
	
	private void exitCarTeamShowDialog(String messages) {
		LayoutInflater inflater = LayoutInflater.from(JionCarTeamInfo.this);
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.alert_dialog, null);
		final Dialog isExit = new AlertDialog.Builder(JionCarTeamInfo.this)
				.create();
		isExit.show();
		isExit.setContentView(layout);
		TextView title = (TextView) layout.findViewById(R.id.title);
		TextView message = (TextView) layout.findViewById(R.id.message);
		title.setText("请注意");
		message.setText(messages);
		Button check = (Button) layout.findViewById(R.id.check);
		Button giveup = (Button) layout.findViewById(R.id.giveup);
		check.setText("确定");
		giveup.setText("取消");
		check.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				isExit.dismiss();
				// updateImg();
				// return true;
				exitCarTeam();
			}
		});
		giveup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				isExit.dismiss();
				// return false;
			}
		});
	}
	

	private void exitCarTeam() {

		HttpJsonTool httpJsonTool = new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					JionCarTeamInfo.this.finish();
				}
			}
		});
		Map<String, String> data = new HashMap<String, String>();
		data.put("carownerid", app.userId);
		data.put("flag", "2");
		L.v("app.userInfoBeen.getLeaderid()",
				"" + app.userInfoBeen.getLeaderid());
		data.put("leaderid", leaderid);
		// data.put(arg0, arg1);
		httpJsonTool.StartWork(data,
		// "http://192.168.1.111:8080/cqtczh/moduls/fleets/deletememberAPP.do"
				GagApi.EXIT_CAR_TEAM, null, null);

	}

}
